import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Pagination, ConfirmService, ToastService, Item, ModalService } from 'ng-tui';
import { ApiData, SearchParams } from '../../../../cores/classes';
import { GlobalService, AuthService , RequestService} from '../../../../cores/services';
import { RechargeService } from '../../services/recharge.service';
import { Recharge } from '../../interfaces/recharge.interfaces';
import { skipWhile, switchMap, map } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { ModalComponent } from './modal.component';
import { HttpConfig } from '../../../../configs/http.config';
@Component({
    templateUrl: './notice.component.html',
    styleUrls: ['./notice.component.scss']
})
export class NoticeComponent implements OnInit {

    theads = ['#', '图标', '一级分类', '创建时间', '操作'];

    search = new SearchParams({ status: '', uid: '', orderSn: '' });

    list = new Array<any>();
    loading = false;
    parentId: number;
    banner: Recharge;
    attribute: number;
    pagination = new Pagination();
    addd = HttpConfig + HttpConfig.SERVER_URL;

    constructor(
        private request: RequestService,
        private activatedRoute: ActivatedRoute,
        private router: Router,
        public global: GlobalService,
        private confirm: ConfirmService,
        private rechargeService: RechargeService,
        private toast: ToastService,
        private modal: ModalService
    ) { }

    ngOnInit() {

        this.loadDatas();
    }



    doSearch() {
        this.pagination.reset();
        this.loadDatas();
    }

    doReset() {
        this.search.clean();
        this.doSearch();
    }

    // loadDatas() {
    //     this.loading = true;

    // }

    loadDatas() {
        this.loading = true;
        this.rechargeService.listGoodsType().subscribe({
            next: res => {
                this.pagination.total = res.datas.total;
                this.list = res.datas;
            },
            complete: () => this.loading = false
        });
    }

    del(id: number) {
        // this.confirm.danger('删除确认', `您确认删除一级分类吗？`)
        //     .pipe(switchMap<void, ApiData>(() => this.rechargeService.del(id)))
        //     .subscribe(() => {
        //         console.log(id);
        //         this.list.splice(this.list.indexOf(id), 1);
        //         this.toast.success('删除成功', `成功删除分类`);
        //         this.loadDatas();
        //     });
    }



    // showEditModal(banner?: Recharge) {
    //     banner = banner || { id:0, src: '', content: '' ,title:''};
    //     const modal = this.modal.create(ModalComponent).open();
    //    this.modal.modal.instance.banner = banner
    //      modal.subscribe(() => {
    //          this.rechargeService.insertBanner(banner).subscribe(() =>{
    //             this.toast.success('保存成功', '成功保存新广告片');
    //             this.loadDatas();
    //          })
    //     })
    // }


    showEditModal(banner?: Recharge) {
        banner = banner || { id: 0, scr: '', content: '', title: '' };
        const modal = this.modal.create(ModalComponent, { center: true });
        modal.instance.banner = banner;
        this.parentId = 0;
        this.attribute = 1;
        modal.open().subscribe(() => {
            this.toast.success('保存成功', '成功保存分类');
            this.loadDatas();
        });
    }

    test(banner?: Recharge) {
        console.log(banner);
    }

}
